package com.rowenci.iot_agriculture.controller;

import com.alibaba.fastjson.JSON;
import com.rowenci.iot_agriculture.bean.Worker;
import com.rowenci.iot_agriculture.bean.MessageToHTML.WorkerMessage;
import com.rowenci.iot_agriculture.bean.admin.Loginlog;
import com.rowenci.iot_agriculture.bean.MessageToHTML.LoginlogMessage;
import com.rowenci.iot_agriculture.bean.iot.Rfid;
import com.rowenci.iot_agriculture.service.api.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/access")
public class AccessController {

    @Resource
    private LoginlogService loginlogService;
    @Resource
    private RfidService rfidService;
    @Resource
    private GreenhouseService greenhouseService;
    @Resource
    private WorkerService workerService;

    @RequestMapping("/showWorkerList")
    public String showWorkerList(Model model) throws Exception {
        List<Worker> workers = workerService.findAll();
        List<WorkerMessage> workerMessages = new ArrayList<>();
        for (int i = 0; i < workers.size(); i++) {
            WorkerMessage workerMessage = new WorkerMessage();
            workerMessage.setId(workers.get(i).getId());
            workerMessage.setName(workers.get(i).getName());
            workerMessage.setRfid(rfidService.findById(workers.get(i).getRfidId()).getCarNumber());
            workerMessages.add(i, workerMessage);
        }
        model.addAttribute("workerMessages", workerMessages);

        return "/pages/access/workerlist";
    }

    @RequestMapping("/showLoginLog")
    public String showLoginLog(Model model) throws Exception {
        List<Loginlog> loginlogs = loginlogService.findAll();
        List<LoginlogMessage> loginlogMessages = new ArrayList<>();

        for (int i = 0; i < loginlogs.size(); i++) {
            //设置发送到前端的数据列表中的对象
            LoginlogMessage loginlogMessage = new LoginlogMessage();
            loginlogMessage.setId(loginlogs.get(i).getId());
            loginlogMessage.setRfid(rfidService.findById(loginlogs.get(i).getRfid_id()).getCarNumber());
            loginlogMessage.setGreenhouse(greenhouseService.findById(loginlogs.get(i).getGreenhouse_id()).get().getId());
            loginlogMessage.setTime(loginlogs.get(i).getTime());
            loginlogMessage.setWorker(workerService.findById(loginlogs.get(i).getWorker_id()).get().getName());

            System.out.println(loginlogMessage);
            loginlogMessages.add(i, loginlogMessage);
        }
        model.addAttribute("loginlogMessages", loginlogMessages);
        return "/pages/access/loginlog";
    }

    @RequestMapping("/showRFID")
    public String showRFID(Model model) throws Exception {
        List<Rfid> rfids = rfidService.findAll();
        model.addAttribute("rfids", rfids);
        return "/pages/access/rfidlist";
    }

    @PostMapping("/addworker")
    @ResponseBody
    public String addworker(String workername, String rfid, Model model) throws Exception {
        Worker worker = new Worker();
        if (rfidService.findByCarNumber(rfid) == null) {
            model.addAttribute("msg", "该卡号不存在");
            return JSON.toJSONString(model);
        }
        worker.setName(workername);
        worker.setRfidId(rfidService.findByCarNumber(rfid).getId());
        if (workerService.addWorker(worker) == false) {
            model.addAttribute("msg", "fault");
        } else {
            model.addAttribute("msg", "success");
        }
        return JSON.toJSONString(model);
    }

    @PostMapping("/deleteworker")
    @ResponseBody
    public String deleteworker(String workername, Model model) throws Exception {
        if (workerService.deleteWorkerByName(workername) == false) {
            model.addAttribute("msg", "fault");
        } else {
            model.addAttribute("msg", "success");
        }
        return JSON.toJSONString(model);
    }

    @PostMapping("/editworker")
    @ResponseBody
    public String editworker(String workername, String rfid, Integer id, Model model) throws Exception {
        Worker worker = new Worker();
        worker.setId(id);
        worker.setName(workername);
        Integer rfid_id = rfidService.findByCarNumber(rfid).getId();
        if (rfid_id == null) {
            model.addAttribute("msg", "fault");
            return JSON.toJSONString(model);
        }
        worker.setRfidId(rfid_id);
        if (workerService.updateWorkerById(worker) == false) {
            model.addAttribute("msg", "fault");
        } else {
            model.addAttribute("msg", "success");
        }
        return JSON.toJSONString(model);
    }

    @PostMapping("/addRFID")
    @ResponseBody
    public String addRFID(String rfid, Model model) throws Exception {
        Rfid rfid1 = new Rfid();
        rfid1.setCarNumber(rfid);
        if (rfidService.add(rfid1) == false) {
            model.addAttribute("msg", "fault");
        } else {
            model.addAttribute("msg", "success");
        }
        return JSON.toJSONString(model);
    }

    @PostMapping("/editRFID")
    @ResponseBody
    public String editRFID(Integer id, String rfid, Model model) throws Exception {
        Rfid rfid1 = new Rfid();
        rfid1.setId(id);
        rfid1.setCarNumber(rfid);
        if (rfidService.updateRfid(rfid1) == false) {
            model.addAttribute("msg", "fault");
        } else {
            model.addAttribute("msg", "success");
        }
        return JSON.toJSONString(model);
    }

    @PostMapping("/delRFID")
    @ResponseBody
    public String delRFID(Integer id, Model model) throws Exception {
        if (rfidService.delRfidById(id) == false) {
            model.addAttribute("msg", "fault");
        } else {
            model.addAttribute("msg", "success");
        }
        return JSON.toJSONString(model);
    }

}
